function myFunction (p1, p2) {
  return p1 * p2;              // 该函数返回 p1 和 p2 的乘积
}


function CreateQueryGeometry () {   //查询后贴图
  var geometry = new THREE.BoxGeometry(410, 100, 100);
  var material = new THREE.MeshLambertMaterial({
    // color: 0x0099ff, // 设置纹理贴图
    color: 0xFFFF00, // 设置纹理贴图  黄色
    transparent: true,
    opacity: 0.5
    // wireframe:true,
  });
  var meshbox = new THREE.Mesh(geometry, material); //网格模型对象Mesh
  return meshbox;
}

function CreateQueryGeometry2 () {   //查询后贴图
  var geometry = new THREE.BoxGeometry(410, 102, 102);
  var material = new THREE.MeshLambertMaterial({
    color: 0x000000, // 设置纹理贴图
    // color: 0x008000, // 设置纹理贴图  
    transparent: true,
    opacity: 0.8
    // wireframe:true,
  });
  var meshbox = new THREE.Mesh(geometry, material); //网格模型对象Mesh
  return meshbox;
}

function CreateSelectGeometry ()   //点击后贴图
{
  var geometry = new THREE.BoxGeometry(410, 100, 100);
  var material = new THREE.MeshLambertMaterial({
    // color: 0x00FFAA, // 设置纹理贴图  
    color: 0xFF0000, // 设置纹理贴图 红色
    transparent: true,
    opacity: 0.5
    // wireframe:true,
  });
  var meshbox = new THREE.Mesh(geometry, material); //网格模型对象Mesh
  return meshbox;
}

//创建各种不同颜色的材质
function createMaterial (color) {
  console.log('folder-color', color);
  var textureLoader = new THREE.TextureLoader(); // 纹理加载器
  var texture = textureLoader.load(`img/${color}/02.png`); // 加载图片，返回Texture对象
  // 材质对象2
  var material_1 = new THREE.MeshLambertMaterial({
    map: texture, // 设置纹理贴图
    // wireframe:true,
  });
  texture = textureLoader.load(`img/${color}/01.png`, function (tmp) {
    rerender();
  }); // 加载图片，返回Texture对象
  // 材质对象2
  var material_2 = new THREE.MeshLambertMaterial({
    map: texture, // 设置纹理贴图
    // wireframe:true,
  });
  //设置材质数组
  var materialArr = [material_1, material_1, material_2, material_2, material_2, material_2];
  return materialArr;
}

function CreateMeshTemplate (folder)  //箱子设置
{
  var geometry = new THREE.BoxGeometry(390, 90, 90); //立方体
  var textureLoader = new THREE.TextureLoader(); // 纹理加载器
  var texture = textureLoader.load(`img/${folder}/02.png`); // 加载图片，返回Texture对象
  // 材质对象2
  var material_1 = new THREE.MeshLambertMaterial({
    map: texture, // 设置纹理贴图
    // wireframe:true,
  });
  texture = textureLoader.load(`img/${folder}/01.png`, function (tmp) {
    rerender();
  }); // 加载图片，返回Texture对象
  // 材质对象2
  var material_2 = new THREE.MeshLambertMaterial({
    map: texture, // 设置纹理贴图
    // wireframe:true,
  });
  //设置材质数组
  var materialArr = [material_1, material_1, material_2, material_2, material_2, material_2];

  // 设置数组材质对象作为网格模型材质参数
  var meshbox = new THREE.Mesh(geometry, materialArr); //网格模型对象Mesh
  return meshbox;
}

//生成从minNum到maxNum的随机数             、、、、、、、、、、、、、、、、、、
// function randomNum(minNum, maxNum) {
//     switch (arguments.length) {
//       case 1:
//         return parseInt(Math.random() * minNum + 1, 10);
//         break;
//       case 2:
//         return parseInt(Math.random() * ( maxNum - minNum + 1 ) + minNum, 10);
//         //或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
//         break;
//       default:
//         return 0;
//         break;
//     }
//   }


function CreateMapGround () {
  // var textureLoader = new THREE.TextureLoader(); // 纹理加载器
  // var groundMaterial = new THREE.MeshLambertMaterial({ color: '#FFFFFF' });
  // var meshground = new THREE.Mesh(new THREE.PlaneBufferGeometry(20000, 20000), groundMaterial);
  // meshground.position.y = 0;
  // meshground.rotation.x = - Math.PI / 2;
  // meshground.receiveShadow = true;
  // return meshground
  var geometryground = new THREE.PlaneGeometry(1000, 1000); //矩形平面
  // 加载草地纹理贴图
  var texturegrass = new THREE.TextureLoader().load("../img/grass.jpg");
  // 设置纹理阵列
  texturegrass.wrapS = THREE.RepeatWrapping;
  texturegrass.wrapT = THREE.RepeatWrapping;
  // uv两个方向纹理重复数量
  texturegrass.repeat.set(10, 10);
  var materialground = new THREE.MeshLambertMaterial({
    map: texturegrass,
  });
  var meshground = new THREE.Mesh(geometryground, materialground); //网格模型对象Mesh
  return meshground;
}

function DrawBaseMap (row, col, left2right, up2down, qudm) {     ///格子
  var length = 100;
  var width = 200;
  var height = 100;
  var canvas = document.createElement("canvas");
  canvas.width = width * (col + 2);
  canvas.height = length * (row + 2);
  //canvas.background = "#0000FF";

  var centerx = canvas.width / 2;
  var centery = canvas.height / 2;
  var cxt = canvas.getContext('2d');

  var mapPosition = new Map();

  cxt.fillStyle = "#FFFFFF";
  cxt.font = "80px Georgia";

  cxt.strokeStyle = "#FFFFFF";
  cxt.lineWidth = 5;

  row = row + 1;
  col = col + 1;
  for (let i = 0; i <= row; i++) {
    for (let j = 0; j <= col; j++) {
      if (i == 0 || j == 0) {
        if (i == 0 && j == 0)
          cxt.fillText(qudm, 0, canvas.height / 2);
        continue;
      }


      if (i == row && j == col)
        continue;
      if (i == row) {
        var bei = j * 2 - 1;
        if (left2right == false) {
          bei = col * 2 - bei;
        }

        cxt.fillText(bei, j * width + width / 2, i * length + length / 2);
        continue;
      }
      if (j == col) {

        var pai = i + 1;
        if (up2down == false)
          pai = row + 1 - pai;
        cxt.fillText(pai, j * width + width / 2, i * length + length / 2);
        continue;
      }



      cxt.strokeRect(j * width, i * length, width, length);

      var rectCenterx = j * width + width;///2;//大箱，所以不除2
      var rectCentery = i * length + length / 2;


      for (let z = 1; z <= 8; z++) { //层数
        var cengheight = height * z - height / 2;
        mapPosition.set(i * 10000 + j * 100 + z, { x: rectCenterx - centerx, z: rectCentery - centery, y: cengheight });

      }


    }
  }

  var texturecanvas = new THREE.CanvasTexture(canvas);
  var canvasMaterial = new THREE.MeshLambertMaterial({
    map: texturecanvas,
    transparent: true
  });

  var meshcanvas = new THREE.Mesh(new THREE.PlaneBufferGeometry(canvas.width, canvas.height), canvasMaterial);
  meshcanvas.position.y = 1;
  meshcanvas.rotation.x = - Math.PI / 2;
  meshcanvas.receiveShadow = true;

  var group = new THREE.Group();
  group.add(meshcanvas);

  //map是每个箱子的坐标，centex,centey是每个组的坐标

  return {
    map: mapPosition,
    grp: group,
    centex: centerx,
    centey: centery,

  };
}